package com.cug.campus_sh_market_server.service.impl;

import com.cug.campus_sh_market_server.entity.myenum.IsDelEnum;
import com.cug.campus_sh_market_server.entity.po.BaseEntity;
import com.cug.campus_sh_market_server.util.MappingUtil;


import java.io.Serializable;

/**
 * @author sxt
 * @version 1.0
 * @description 适用于BaseEntity的RestfullService抽象实现
 * @date 2022/3/29 21:36
 */
public abstract class CustomRestfullServiceImpl
        <Entity extends BaseEntity, QueryParam extends BaseEntity, SaveParam extends BaseEntity, UpdateParam extends BaseEntity, VO extends BaseEntity>
        extends AbsRestfullServiceImpl<Entity, QueryParam, SaveParam, UpdateParam, VO> {
    @Override
    public boolean saveData(SaveParam saveParam) {
        Entity entity = MappingUtil.getMappingObject(saveParam, entityClass);
        entity.create();
        return super.save(entity);
    }

    @Override
    public VO saveWithBackData(SaveParam saveParam) {
        Entity entity = MappingUtil.getMappingObject(saveParam, entityClass);
        entity.create();
        int rs = baseMapper.insert(entity);
        if (rs > 0) {
            return voBaseMapper.selectById(getEntityId(entity));
        }
        return null;
    }

    @Override
    public boolean removeData(Serializable id) {
        if (id == null) {
            return false;
        }
        Entity entity = super.getById(id);
        if (entity == null) {
            return false;
        }
        entity.setDel(IsDelEnum.YES.getCode());
        entity.modify();
        return super.updateById(entity) && entity.deleteById();
    }

    @Override
    public boolean updateData(UpdateParam updateParam) {
        Entity entity = MappingUtil.getMappingObject(updateParam, entityClass);
        entity.modify();
        return super.updateById(entity);
    }
}
